//
//  TriangleView.m
//  

//
//  Created by Gang.Wang on 12-5-22.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "TriangleView.h"
#import "mcf.h"

@interface TriangleView ()
{
    
    CGContextRef mCtx;
    
    CGPoint trianglePoint1;
    CGPoint trianglePoint2;
    CGPoint trianglePoint3;
}
- (void) privateInit;

@property (nonatomic, assign) CGPoint trianglePoint1;
@property (nonatomic, assign) CGPoint trianglePoint2;

@property (nonatomic, assign) CGPoint trianglePoint3;
@end

@implementation TriangleView

@synthesize direction;
@synthesize trianglePoint1, trianglePoint2, trianglePoint3;




- (void) dealloc
{   
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    if (frame.size.width != frame.size.height) 
    {
        return nil;
    }
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];        
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame withTriangleDirection:(YTriangleDirection ) directionValue
{
    self = [super initWithFrame:frame];
    if (self) {
        self.direction = directionValue;
        [self setBackgroundColor:COLOR_CLEAR];
        
        [self privateInit];
    }
    return self;
}

#pragma mark - Private Method
- (void) privateInit
{
    if(self.bInited)
    {
        return;
    }
       
    [self setBackgroundColor:COLOR_CLEAR];
        
    [self reAdjustLayout];
    mCtx = UIGraphicsGetCurrentContext();  
    self.bInited = TRUE;        
}


#pragma mark - Publish Method

- (void) reAdjustLayout
{    
    switch (self.direction) 
    {
        case kTriangleDirectionUp:
            self.trianglePoint1 = CGPointMake(self.width / 2, 0);
            self.trianglePoint2 = CGPointMake(0, self.height);        
            self.trianglePoint3 = CGPointMake(self.width, self.height);

            break;
            
        case kTriangleDirectionDown:
            self.trianglePoint1 = CGPointMake(0, 0);
            self.trianglePoint2 = CGPointMake(self.width / 2, self.height);        
            self.trianglePoint3 = CGPointMake(self.width, 0);
            
            break;
            
        case kTriangleDirectionLeft:
            self.trianglePoint1 = CGPointMake(0, self.height / 2);
            self.trianglePoint2 = CGPointMake(self.width, self.height);        
            self.trianglePoint3 = CGPointMake(self.width, 0);
            
            break;
            
        case kTriangleDirectionRight:
            self.trianglePoint1 = CGPointMake(0, 0);
            self.trianglePoint2 = CGPointMake(0, self.height);        
            self.trianglePoint3 = CGPointMake(self.width, self.height / 2);
            
            break;
            
        default:
            break;
    }
    
    [self setNeedsDisplay];
    
}


- (BOOL) isVisibleAtPoint:(CGPoint)point
{
    BOOL result = [YMath pointIsInTriangle:point withTrianglePoint1:self.trianglePoint1 
                        withTrianglePoint2:self.trianglePoint2 
                        withTrianglePoint3:self.trianglePoint3];

    DebugLog(@"%d  %@", result, self.description);
    return result;
}

- (void) drawTriangle
{        
    UIBezierPath* path = [UIBezierPath bezierPath];   
    [path moveToPoint:self.trianglePoint1];
    [path addLineToPoint:self.trianglePoint2];
    [path addLineToPoint:self.trianglePoint3];
    [path closePath];    
//    [path fill];
//    [path stroke];
    [path addClip];
    CGContextAddPath(mCtx, path.CGPath);
    
    CGContextClip(mCtx);
//    CGContextEOClip(mCtx);
    
    [image drawAtPoint:CGPointZero];
        
    CGContextRestoreGState(mCtx);  
}


- (void) drawRect:(CGRect)rect
{    
    CGContextSaveGState(mCtx);
    [self drawTriangle];
}
 
@end
